package me.zhengjie.modules.system.service.dto

import cn.hutool.core.bean.BeanUtil
import cn.hutool.core.bean.copier.CopyOptions
import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.annotation.JsonTypeInfo
import com.fasterxml.jackson.annotation.JsonTypeName
import jakarta.validation.constraints.NotNull
import me.zhengjie.common.utils.JacksonUtil
import java.sql.Timestamp
import java.util.*

/**
 * @author Kuki Wu
 * @date 2019-03-25
 */

@JsonTypeName("DeptDTO")
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS)
data class DeptDTO(
    // ID
    var id: Long? = null,

    // 名称
    var name: String? = null,

    var enabled: Boolean? = null,

    var deptSort: Integer? = null,

    @get:JsonTypeInfo(use = JsonTypeInfo.Id.CLASS)
    @get:JsonInclude(JsonInclude.Include.NON_EMPTY)
    var children: MutableSet<DeptDTO>? = mutableSetOf(),

    // 上级部门
    var pid: Long? = null,

    var subCount: Int? = null,

    var label: String? = name

){
    annotation class Update

//    fun getLabel(): String {
//        return name!!
//    }

    fun getHasChildren(): Boolean {
        return subCount!! > 0
    }

    fun getLeaf(): Boolean {
        return subCount!! <= 0
    }

    override fun equals(o: Any?): Boolean {
        if (this === o) {
            return true
        }
        if (o == null || javaClass != o.javaClass) {
            return false
        }
        val deptDto: DeptDTO =
            o as DeptDTO
        return id == deptDto.id && name == deptDto.name
    }

    override fun hashCode(): Int {
        return Objects.hash(id, name)
    }

    companion object {
        fun fromJson(json: String): DeptDTO? {
            val obj=JacksonUtil.parseObject(json, DeptDTO::class.java)!!
            return when(obj){
                is DeptDTO -> obj
                else -> null
            }
        }
    }

    fun tojson():String? {
        return JacksonUtil.toJSONString(this)
    }


    fun copy(source: DeptDTO?) {
        BeanUtil.copyProperties(source, this, CopyOptions.create().setIgnoreNullValue(true))
    }
}